package com.ruitu.mall_98_2.mine.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.Company;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FragmentAttention extends MyRefreshBaseFragment {
    private DataAdapter adapter;
    private List<Integer> skipId = new ArrayList<>();
    private String id;

    public static FragmentAttention newInstance() {
        Bundle args = new Bundle();
        FragmentAttention fragment = new FragmentAttention();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getInflate() {
        return R.layout.fragment_mine_three;
    }

    @Override
    protected LRecyclerView getRecyclerView(View view) {
        return view.findViewById(R.id.lRecyclerView);
    }

    @Override
    protected ProgressLayout findProgressLayout(View view) {
        return view.findViewById(R.id.progress);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        reReq();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {
        showProgress();
        getCollection();
    }

    @Override
    protected void initRecyclerView(View v) {
        super.initRecyclerView(v);
    }

    class DataAdapter extends ListBaseAdapter<Company> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_company;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_company_img = holder.getView(R.id.iv_company_img);
            TextView tv_company_name = holder.getView(R.id.tv_company_name);
            TextView tv_sal_num = holder.getView(R.id.tv_sal_num);
            TextView tv_saling = holder.getView(R.id.tv_saling);
            LinearLayout ll_company = holder.getView(R.id.ll_company);
            final Company company = mDataList.get(position);
            Glide.with(mContext).load(Config.BASE_URL + company.getCompanyUrl()).apply(Arad.opts).into(iv_company_img);
            tv_company_name.setText(company.getName());
            if (TextUtils.isEmpty(company.getTotalGoods())) {
                tv_sal_num.setText("在售商品 " + 0 + "件");
            } else {
                tv_sal_num.setText("在售商品 " + company.getTotalGoods() + "件");
            }

            tv_saling.setText("经营范围:" + company.getCompanyInfo().getCompanyKey());
            ll_company.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("companyId", company.getId());
                    try {
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ShopDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });

            ll_company.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    id = company.getCollectionId();
                    showCancleDialog();
                    return false;
                }
            });
        }

    }

    public void getCollection() {
        APIRetrofit.getDefault().getCompanyList(AppHolder.instance().getUser().getAccountId(), "2")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<List<Company>>>() {
                    @Override
                    public void onError(Throwable e) {
                        refreshComplete();
                        hideProgress();
                        Logg.i(e.toString());
                        ToastUtils.showShort("获取关注列表失败");
                        showError(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                reReq();
                            }
                        });
                    }

                    @Override
                    public void onNext(BaseResult<List<Company>> baseResult) {
                        refreshComplete();
                        hideProgress();
                        if (baseResult.isSuccess()) {
                            if (isDataEmpty(baseResult.getData())) {
                                showEmptyView(skipId);
                            } else {
                                adapter.setDataList(baseResult.getData());
                                showContent(null);
                            }
                        }
                    }
                });
    }

    private void cancelCollection() {
        APIRetrofit.getDefault().cancelCollection(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        ToastUtils.showLong("取消关注异常");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        hideProgress();
                        if (baseResult.isSuccess()) {
                            ToastUtils.showLong(baseResult.getMsg());
                            reReq();
                        } else {
                            ToastUtils.showLong(baseResult.getMsg());
                        }
                    }
                });
    }

    private void showCancleDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
        View view = View.inflate(getContext(), R.layout.dialog_cancle_collection, null);
        TextView tv_cancle = view.findViewById(R.id.tv_cancle);
        tv_cancle.setText("取消关注");
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelCollection();
            }
        });
        alertDialog.setView(view);
        Window window = getActivity().getWindow();
        window.setGravity(Gravity.CENTER);
        alertDialog.show();
    }

}
